<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;

/**
 * Created by PhpStorm.
 * User: guota
 * Date: 2018/8/29
 * Time: 22:54
 */
class CompanyController extends AdminBaseController {

    public function index(){

        $list = db('company')->paginate(20);
        $page = $list->render();

        return $this->fetch('',array(
            'list' => $list,
            'page' => $page
        ));
    }

    public function add(){
        $list = db('user')->field('id, user_login')->paginate('20');
        return $this->fetch('', array(
            'users' => $list
        ));
    }

    public function addPost(){
        $data = $this->request->param();
        $data['create_time'] = time();
        $rest = db('company')->insert($data);
        if (!$rest){
            $this->error('添加失败');
        }
        $this->success('添加成功');
    }

    public function edit(){
        $id = $this->request->param('id');
        $info = db('company')->where('id', $id)->find();
        $list = db('user')->field('id, user_login')->paginate('20');
        return $this->fetch('', array(
            'users' => $list,
            'info' => $info
        ));
    }

    public function editPost(){
        $data = $this->request->param();
        $data['update_time'] = time();
        $rest = db('company')->update($data);
        if (!$rest){
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    public function delete($id){
        $rest = db('company')->delete($id);
        if (!$rest){
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

}
